package com.example.core;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.example.core.controller.BaseController;
import com.example.core.entity.BaseEntity;
import com.example.core.mapper.BaseMapper;
import com.example.core.service.IService;
import com.example.core.service.serviceImpl.ServiceImpl;

import java.util.Collections;
import java.util.Scanner;

public class CodeGenerator {

    public static final String URL = "jdbc:mysql://th411412.cn:3306/xun";

    public static final String USER_NAME = "dev";

    public static final String USER_PASSWORD = "dev!1234";

    public static final String AUTHOR = "LiJun";

    public static String CREATE_MODE = "create";


    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入需要生成的表，用','隔开。");
        String[] tables = scanner.next().split(",");
        System.out.println("请输入使用模式，eg：create,other...");
        String mode = scanner.next();

        FastAutoGenerator.create(URL, USER_NAME, USER_PASSWORD)
                .globalConfig(builder -> {
                    builder.author(AUTHOR)
                            .outputDir(System.getProperty("user.dir") + "/src/main/java")
                            .dateType(DateType.ONLY_DATE)
                            .commentDate("yyyy-MM-dd hh:mm:ss")
                            .fileOverride();

                })
                .packageConfig(builder -> {
                    builder.parent("com.example.core")
                            .entity("entity")
                            .service("service")
                            .serviceImpl("service.serviceImpl")
                            .controller("controller")
                            .mapper("mapper")
                            .xml("mapper")
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, System.getProperty("user.dir") + "/src/main/java/com/example/smartant/mapper"));
                })
                .strategyConfig(builder -> {
                    StrategyConfig.Builder configBuilder = builder.addInclude(tables)
                            .addTablePrefix("th_");
                    configBuilder.entityBuilder()
                            .superClass(BaseEntity.class)
                            .enableLombok()
                            .enableTableFieldAnnotation();
                    if (CREATE_MODE.equals(mode)) {
                        configBuilder.serviceBuilder()
                                .formatServiceFileName("%sService")
                                .superServiceClass(IService.class)
                                .superServiceImplClass(ServiceImpl.class)
                                .formatServiceImplFileName("%sServiceImpl");
                        configBuilder.controllerBuilder()
                                .formatFileName("%sController")
                                .superClass(BaseController.class)
                                .enableRestStyle();
                        configBuilder.mapperBuilder()
                                .superClass(BaseMapper.class)
                                .formatMapperFileName("%sMapper")
                                .enableBaseResultMap()
                                .enableMapperAnnotation()
                                .formatXmlFileName("%sMapper");
                    }

                })
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }
}

